﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    public class PersonalSettingRepository : IPersonalSettingRepository
    {
        private readonly AgricultureDbContext _dbcontext;

        public PersonalSettingRepository(AgricultureDbContext dbcontext)
        {
            _dbcontext = dbcontext;
        }
        /// <summary>
        /// 根据用户Id获取个人信息
        /// </summary>
        /// <param name="uId">用户Id</param>
        /// <returns></returns>
        public Userinfo GetPersonalDetails(int uId)
        {
            var userinfo = _dbcontext.Userinfos.Find(uId);
            return userinfo;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldPassword">旧密码</param>
        /// <param name="newPassword">新密码</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> UpdatePassWord(string newPassword, string oldPassword)
        {
            var userinfo = _dbcontext.Userinfos.Find(1);
            if (newPassword != userinfo.Upwd)
            {
                //旧密码错误
                return -1;
            }
            userinfo.Upwd = oldPassword;
            _dbcontext.Userinfos.Update(userinfo);
            return await _dbcontext.SaveChangesAsync();
        }
    }
}
